﻿using Prism.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using static EONFULLSIN2.Events.MessageEvent;

namespace EONFULLSIN2.View
{
    /// <summary>
    /// MainView.xaml 的交互逻辑
    /// </summary>
    public partial class MainView : UserControl
    {
        public MainView(IEventAggregator eventAggregator)
        {
            InitializeComponent();

            //订阅日志事件
            eventAggregator.GetEvent<ConnectLogEvent>().Subscribe(ConnectLogUIEvent);
            eventAggregator.GetEvent<RunLogEvent>().Subscribe(RunLogUIEvent);
        }

        //连接日志UI
        public void ConnectLogUIEvent(string logMessage)
        {
            App.Current.Dispatcher.InvokeAsync(() =>
            {
                if (logConnRichTextBox.Document.Blocks.Count > 1000)
                {
                    logConnRichTextBox.Document.Blocks.Clear();
                }
                logConnRichTextBox.AppendText($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} => " + logMessage.Replace(",", "，") + Environment.NewLine);
                logConnRichTextBox.ScrollToEnd();
            });
        }

        public void RunLogUIEvent(string logMessage) {
            App.Current.Dispatcher.InvokeAsync(() =>
            {
                if (logRunRichTextBox.Document.Blocks.Count > 1000)
                {
                    logRunRichTextBox.Document.Blocks.Clear();
                }
                logRunRichTextBox.AppendText($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} => " + logMessage.Replace(",", "，") + Environment.NewLine);
                logRunRichTextBox.ScrollToEnd();
            });
        }
    }
}
